package Servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

import Bean.Car;
import Bean.User;
import Service.CarService;
import ServiceImpl.CarServiceImpl;

/**
 * Servlet implementation class AddCarServlet
 */
public class AddCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public AddCarServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html charset = UTF-8");
		CarService carservice = new CarServiceImpl();
		Car car = new Car();
		User user = new User();
		String username=request.getParameter("username");
		String car_id=request.getParameter("car_id");
		car.setCar_id(car_id);
		user.setUsername(username);
		if(carservice.saveAddCar(user, car)) {
			// 成功
			System.out.println("Add car success!");
			request.getRequestDispatcher("admin_info.jsp").forward(request, response);
		} else {
			// 不成功
			System.out.println("Add car wrong!");
			request.getRequestDispatcher("add_car_info.jsp").forward(request, response);
		}
	}

}
